package cn.com.webConfiguration;


import cn.com.filter.CorsFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;

@Configuration
public class FilterConfig
{

    @Bean
    public FilterRegistrationBean encodingFilter(){
        FilterRegistrationBean filterBean=new FilterRegistrationBean();
        CharacterEncodingFilter encodingFilter=new CharacterEncodingFilter();
        encodingFilter.setEncoding("utf8");
        filterBean.setFilter(encodingFilter);
        filterBean.setName("encodingFilter");
        filterBean.addUrlPatterns("/*");
        filterBean.setOrder(1);
        return filterBean;
    }

    @Bean
    public FilterRegistrationBean corsFilter(){
        FilterRegistrationBean filterBean=new FilterRegistrationBean();
        CorsFilter corsFilter=new CorsFilter();
        filterBean.setFilter(corsFilter);
        filterBean.setName("corsFilter");
        filterBean.addUrlPatterns("/*");
        filterBean.setOrder(2);
        return filterBean;
    }

    @Bean
    public ServletRegistrationBean dispatchServletRegister(DispatcherServlet dispatcherServlet){
        ServletRegistrationBean servletBean=new ServletRegistrationBean();
        servletBean.setName("dispatchServlet");
        servletBean.setServlet(dispatcherServlet);
//        servletBean.addUrlMappings("/front/*","/back/*");
        servletBean.addUrlMappings("/*");
        servletBean.setLoadOnStartup(1);
        return servletBean;
    }



}

